{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4b9c3505-46d7-41e2-8a73-f935fc477b2e",
   "metadata": {},
   "source": [
    "## 前期准备\n",
    "\n",
    "alice 和 bob 节点分别启动 ray 集群。\n",
    "\n",
    "```\n",
    "ray start --head --node-ip-address=\"ip\" --port=\"port\" --include-dashboard=False --disable-usage-stats\n",
    "```\n",
    "\n",
    "屏幕输出中显示 <span style=\"color: #008A00\">Ray runtime started</span>，则说明 Ray 的主节点启动成功。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48ef05a4-9b0c-462b-b492-59b3ed0a8daa",
   "metadata": {},
   "source": [
    "## 初始化 SecretFlow\n",
    "\n",
    "alice 和 bob 节点都需要初始化 secretflow，初始化语句只有在 self_party 上有区别。\n",
    "\n",
    "1. 分别使用 Ray 主节点的 node-ip-address 和 port 填充 sf.init 的 address 参数。\n",
    "2. parties 中 alice 和 bob 的 address 分别填可以被对方访问的地址，并且选择一个**未被占用的端口。**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8bb69b61-3c46-473d-bc09-63458d1d74c3",
   "metadata": {
    "execution": {}
   },
   "outputs": [],
   "source": [
    "import secretflow as sf\n",
    "import spu\n",
    "\n",
    "network_conf = {\n",
    "    \"parties\": {\n",
    "        \"alice\": {\n",
    "            \"address\": \"alice:8080\",\n",
    "        },\n",
    "        \"bob\": {\n",
    "            \"address\": \"bob:8080\",\n",
    "        },\n",
    "    },\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a96168de-6e3d-4867-a802-1f67729d47bf",
   "metadata": {},
   "source": [
    "### alice 和 bob 初始化\n",
    "\n",
    "<span style=\"color: #BA4081\">注意：下面初始化语句是分别只在对应的 alice 或 bob 节点执行的。除了初始化语句，后面所有代码，会被同时下发到 alice 和 bob 节点执行。</span>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "115b9ebf-4d2c-4004-ad54-b79f4d6da2a6",
   "metadata": {
    "execution": {}
   },
   "outputs": [],
   "source": [
    "sf.shutdown()\n",
    "sf.init(\n",
    "    address=\"127.0.0.1:6379\",\n",
    "    cluster_config={**network_conf, \"self_party\": \"alice\"},\n",
    "    log_to_driver=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb953a01-f34e-4c29-a562-ee8c9975eb05",
   "metadata": {
    "execution": {}
   },
   "outputs": [],
   "source": [
    "sf.shutdown()\n",
    "sf.init(\n",
    "    address=\"127.0.0.1:6379\",\n",
    "    cluster_config={**network_conf, \"self_party\": \"bob\"},\n",
    "    log_to_driver=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6da7bf4-0e7a-4a92-9a89-a54aa6ab31e2",
   "metadata": {},
   "source": [
    "## 初始化 SPU\n",
    "\n",
    "1. alice 的 address 请填写可以被 bob 访通的地址，并且选择一个**未被占用的端口** ，注意不要和 Ray 端口冲突。\n",
    "2. alice 的 listen_addr 可以和 alice address 里的端口一样。\n",
    "3. bob 的 address 请填写可以被 alice 访通的地址，并且选择一个**未被占用的端口** ，注意不要和 Ray 端口冲突。\n",
    "4. bob 的 listen_addr 可以和 bob address 里的端口一样。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4a3d7363-fc68-489d-afb1-3723010e5a01",
   "metadata": {
    "execution": {}
   },
   "outputs": [],
   "source": [
    "alice, bob = sf.PYU(\"alice\"), sf.PYU(\"bob\")\n",
    "spu_conf = {\n",
    "    \"nodes\": [\n",
    "        {\"party\": \"alice\", \"address\": \"alice:8081\", \"listen_addr\": \"alice:8081\"},\n",
    "        {\"party\": \"bob\", \"address\": \"bob:8081\", \"listen_addr\": \"alice:8081\"},\n",
    "    ],\n",
    "    \"runtime_config\": {\n",
    "        \"protocol\": spu.spu_pb2.SEMI2K,\n",
    "        \"field\": spu.spu_pb2.FM128,\n",
    "        \"sigmoid_mode\": spu.spu_pb2.RuntimeConfig.SIGMOID_REAL,\n",
    "    },\n",
    "}\n",
    "spu = sf.SPU(cluster_def=spu_conf)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc12b191-abdb-43f6-86f3-06b06e13b3ba",
   "metadata": {},
   "source": [
    "## 隐私求交\n",
    "\n",
    "<span style=\"color: rgb(50, 50, 50)\">提供 </span>`psi_csv`<span style=\"color: rgb(50, 50, 50)\"> 函数， </span>`psi_csv`<span style=\"color: rgb(50, 50, 50)\"> 将 csv 文件作为输入，并在求交后生成 csv 文件。默认协议为 </span>[**KKRT**](https://eprint.iacr.org/2016/799.pdf)<span style=\"color: rgb(50, 50, 50)\">。</span>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "293ad974-0904-4955-b1fa-8649331d40dd",
   "metadata": {
    "execution": {}
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "current_dir = os.getcwd()\n",
    "\n",
    "input_path = {\n",
    "    alice: f\"{current_dir}/iris_alice.csv\",\n",
    "    bob: f\"{current_dir}/iris_bob.csv\",\n",
    "}\n",
    "output_path = {\n",
    "    alice: f\"{current_dir}/iris_alice_psi.csv\",\n",
    "    bob: f\"{current_dir}/iris_alice_psi.csv\",\n",
    "}\n",
    "spu.psi_csv(\"uid\", input_path, output_path, \"alice\")"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
